package com.example.springworkback.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springworkback.Dao.InspectorsRepository;
import com.example.springworkback.Entity.Inspectors;
import com.example.springworkfront.Dao.Healthy_statusRepository;
import com.example.springworkfront.Dao.HouseholdRepository;
import com.example.springworkfront.Entity.Healthy_status;
import com.example.springworkfront.Entity.Household;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

@Service
public class InspectorsServiceImpl implements InspectorsService {
    @Autowired
    private InspectorsRepository inspectorsRepository;
    @Autowired
    private Healthy_statusRepository healthy_statusRepository;
    @Autowired
    private HouseholdRepository householdRepository;
    @Override
    public boolean login(String account,String password){
        Inspectors inspectors=inspectorsRepository.findUserByUsernameAndPassword(account,password);
        if (inspectors!=null){
            return true;
        }
        return false;
    }
    @Override
    @Transactional
    public void add(String account,float temperature,String place,String date,String status){
        Household household=householdRepository.findById(account);
        String json= JSON.toJSONString(household);
        JSONObject jo = JSON.parseObject(json);
        String name=jo.getString("name");
        String password=jo.getString("password");
        String img=jo.getString("img");
        String phone=jo.getString("phone");
        String idCard=jo.getString("idCard");
        String roomNumber=jo.getString("roomNumber");
        String accessTimes=jo.getString("accessTimes");
        String openid=jo.getString("openid");
        int times=Integer.valueOf(accessTimes);
        if(!status.equals("异常")){times=times+1;}
        Household update=new Household(account,name,password,img,phone,idCard,roomNumber,times,openid,status);
        householdRepository.update(account,times,status);
        Healthy_status healthy_status=new Healthy_status(account,temperature,place,date,status);
        healthy_statusRepository.save(healthy_status);
    }
}
